<template>
	<view class="content">
		<navBar>
			<template @click="backPage()" slot='left'>
				<u-icon name="arrow-left" color="#fff"></u-icon>
			</template>
			<template class="centerText" slot='center'>
				<view class="" style="color: #fff;">
					{{title}}
				</view>
			</template>
		</navBar>
		<!-- 信息录入 -->
		<view class="information_entry">
			<u--form labelPosition="left" ref="uForm" labelWidth='80' :model="formData" :rules="rules">
				<u-form-item required label="品牌名称" prop="brand_name" borderBottom ref="item1">
					<u--input v-model="formData.brand_name" disabledColor="#ffffff" placeholder="请输入品牌名称"
						border="none"></u--input>
				</u-form-item>
				<u-form-item label="供应商" prop="supplier" borderBottom ref="item1">
					<u--input v-model="formData.supplier" disabledColor="#ffffff" placeholder="请输入供应商"
						border="none"></u--input>
				</u-form-item>
				<u-form-item label="联系人" prop="supplier_contact" borderBottom ref="item1">
					<u--input v-model="formData.supplier_contact" disabledColor="#ffffff" placeholder="请输入联系人"
						border="none"></u--input>
				</u-form-item>
				<u-form-item label="联系方式" prop="supplier_phone" borderBottom ref="item1">
					<u--input v-model="formData.supplier_phone" disabledColor="#ffffff" placeholder="请输入联系方式"
						border="none"></u--input>
				</u-form-item>
				<u-form-item required label="品牌顺序" prop="unit" borderBottom ref="item1">
					<u-number-box v-model="formData.order_num">
						<view slot="minus" class="minus">
							<u-icon name="minus" size="12" color="#f9b85c"></u-icon>
						</view>
						<text slot="input" style="width: 50px;text-align: center;"
							class="input">{{formData.order_num}}</text>
						<view slot="plus" class="plus">
							<u-icon name="plus" color="#ff0000" size="12"></u-icon>
						</view>
					</u-number-box>
				</u-form-item>
				<u-form-item @click="selectStatus()" label="品牌状态" prop="cargoCode" borderBottom ref="item1">
					{{columns[0][formData.status-1].label}}
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="备注信息" borderBottom ref="item1">
					<u--textarea v-model="formData.remark" maxlength="50" count placeholder="请输入内容"></u--textarea>
				</u-form-item>

			</u--form>
		</view>
		<!-- 提交 -->
		<view class="bottom_box">
			<view class="submit_btn">
				<u-button @click="saveSubmit()" color="#39cccc" shape='circle' type="primary" text="保存"></u-button>
			</view>
		</view>
		<!-- 状态选择 -->
		<u-picker :show="showStatus" :columns="columns" keyName="label" @cancel='handelCancel'
			@confirm='handelConfirm()' @close='pickerClose()'></u-picker>
	</view>
</template>

<script>
	import navBar from '@/components/navBar.vue'
	import {
		addBrandApi,
		editBrandApi
	} from "@/api/brandManage.js"
	export default {
		data() {
			return {
				formData: {
					brand_name: "", //品牌名
					supplier: "", //供应商名字
					supplier_contact: "", //供应商联系人
					supplier_phone: "", //供应商电话
					order_num: 1, //排序ID 值越小越靠前
					status: 1, //状态:1启用 2 禁用
					remark: ""
				},
				title: '新增品牌',
				showPopup: false,
				showStatus: false,
				columns: [
					[{
						label: '正常',
					}, {
						label: '停用',
					}]
				],
				rules: {
					brand_name: {
						required: true,
						message: '请输入供应商',
						trigger: ['blur'],
					}
				},
			};
		},
		onLoad(e) {
			if (e.brandInfo) {
				this.formData = JSON.parse(e.brandInfo)
				this.title = '编辑品牌'
			}
		},
		components: {
			navBar,
		},
		methods: {
			backPage() {
				uni.navigateBack();
			},
			// 选择类别
			selectCategory() {
				this.showPopup = true
			},
			//保存
			async saveSubmit() {
				await this.$refs.uForm.validate()
				this.title == '编辑品牌' ? await editBrandApi(this.formData) : await addBrandApi(this.formData)
				this.title == '编辑品牌' ? uni.$u.toast('编辑成功') : uni.$u.toast('新增成功')
				setTimeout(() => {
					this.backPage()
				}, 1000)
			},
			//选择状态
			selectStatus() {
				this.showStatus = true
			},
			//关闭
			pickerClose() {
				this.showStatus = false
			},
			//确定
			handelConfirm(e) {
				this.formData.status = e.indexs[0] + 1
				this.pickerClose()
			},
			//取消
			handelCancel() {
				this.pickerClose()
			}
		}
	}
</script>

<style lang="scss">
	.content {
		height: 100vh;
		background-color: #e0ecfa;

		.information_entry {
			background-color: #fff;
			margin: 60rpx 10rpx 0;
			padding: 30rpx 20rpx;
			border-radius: 20rpx;

			::v-deep .u-form-item__body__left {
				margin-left: 20rpx;
			}
		}

		.bottom_box {
			display: flex;
			justify-content: center;
			align-items: center;
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 160rpx;
			background-color: #f0f3f8;

			.submit_btn {
				width: 600rpx;
			}
		}

		.minus {
			width: 44rpx;
			height: 44rpx;
			border: 1px solid #f9b85c;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.input {
			// padding: 0 10rpx;
		}

		.plus {
			width: 44rpx;
			height: 44rpx;
			border: 1px solid #ff0000;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>